home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / write your own Drag_on / Code_Main.c < prev    next >
C/C++ Source or Header  |  1995-01-13  |  10KB  |  402 lines

  1. /* Code_Main.c : file containing main() for code resource */
  2. /* GENERIC VERSION - modify to create your own code resources */
  3.  
  4. /* Copyright © 1991, 1992 the Free Software Foundation, Inc.
  5.  *         This file is free software; you can redistribute or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or any later version.
  8.  *         This file is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *         You should have received a copy of the GNU General Public License
  13.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  14.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  16.    Revised May 1992: new setup/restore a4 strategy.
  17.  */
  18.  
  19. /* Required modifications: change InvokeResource to the name of the
  20. function that starts off your code resource. It should return an short
  21. as result code, and take no arguments. 
  22. See CHANGE THIS below.*/
  23.  
  24. /* Your "Invoke" function should return the following result codes:
  25. <= -3 : counts as -1 at present
  26. -2 : show stderr
  27. -1 : user cancelled or error during dialog - no run
  28. 0  : run OK, do nothing special after
  29. 1  : show stdout
  30. 2  : show and select stdout
  31. > 2 : no action at present (counts as equivalent to 0)
  32. */
  33.  
  34.  
  35. /* Files forming the basis of a new code resource:
  36. Source files:
  37. Code_Main.c
  38. CodeResource_Helper.c
  39. CodeResource_Files.c - for Macintosh-style file i/o
  40. Header files:
  41. AppCodeComm.h
  42. CodeResource.h
  43. CodeResHelper.h - prototypes for helper functions
  44. CodeResFiles.h - prototypes for read/write functions 
  45.                 in CodeResource_Files.c
  46. -and a4 versions of ANSI unix etc libraries if needed.
  47.  
  48. If your code resource will be large, place Code_Main.c in a segment
  49. by itself. The segment containing Code_Main.c also contains your
  50. jump table and global variables, so the code there needs to be kept
  51. as small as possible.
  52. */
  53.  
  54. #include "AppCodeComm.h"
  55. #include <string.h>
  56.  
  57. /* Global copy of the AppCodeComm struct passed from the application
  58. to the code resource - gacc stands for Global Application-Code resource
  59. Communication. */
  60. AppCodeComm    gacc;
  61.  
  62. /* Global to indicate if calling app's event loop has been made
  63. available and should call it */
  64. Boolean gConcurrent;
  65.  
  66. #ifndef NULL
  67. #define NULL        ((void *) 0)
  68. #endif
  69.  
  70. long _code_a4, _app_a4;
  71.  
  72. #define SetUpA4()    asm { move.l a4,_temp_a }\
  73.                     asm { move.l _temp_c, a4 }\
  74.                     _app_a4 = _temp_a;
  75.                             
  76.  
  77. #define RestoreA4()        long    _temp_a, _temp_c = _code_a4;\
  78.                         asm { move.l _app_a4,a4 }
  79. /*
  80. -call RestoreA4() at very beginning of callback
  81. -at end of each callback do SetUpA4()
  82. -there's also a bit of asm at the top of main() to recover a0,
  83.     which contains the Code's a4 value, and at bottom to restore a4.
  84. */
  85.  
  86. /* "main" call for the specific code resource */
  87.             CHANGE THIS
  88. extern short     InvokeResource(void); /* see ??.c */
  89.  
  90. /* Protos for functions in this file */
  91. void main(ACCPtr ac);
  92. /* Callbacks, to be made available to other files */
  93. short InDictionary(char *tokenName);
  94. Boolean HasInDictionary(void);
  95. Handle GetFrontText(Boolean getItAll);
  96. Boolean HasGetFrontText(void);
  97. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  98.         short *vRefNumPtr, char *fileName, Boolean clearFlag);
  99. Boolean HasGetNextMultiFile(void);
  100. short OKStopAlert(Ptr cstringPtr);
  101. Boolean HasOKStopAlert(void);
  102. void MemoryAlert(void);
  103. Boolean HasMemoryAlert(void);
  104. short GetScreenHeight(void);
  105. Boolean HasGetScreenHeight(void);
  106. short GetScreenWidth(void);
  107. Boolean HasGetScreenWidth(void);
  108. void SetWatchCursor(void);
  109. Boolean HasSetWatchCursor(void);
  110. void DoEventLoopOnce(void);
  111. Boolean HasDoEventLoopOnce(void);
  112. Handle GetTheClip(void);
  113. Boolean HasGetTheClip(void);
  114. short PutTheClip(char *newClipStr);
  115. Boolean HasPutTheClip(void);
  116.  
  117. /* The "main" that is called from the running application. This file is
  118. kept small, and best in its own segment because lots of other things get piled
  119. into here as well - the jump table for the code resource, for example.
  120. So do the bare minimum - set up the global register reference, call the
  121. code resource main function, pass back a result code, and restore the
  122. a4 register. Functions below main() are the extension interface
  123. functions, which must be in this file to make use of the little functions
  124. above that save and restore a4. They all check to see that the extension
  125. in question has been passed from the application, and if not either return
  126. a zero or NULL or return a safe general value. No extension should be
  127. essential to the operation of the code resource.
  128. */
  129. /* results:
  130. <= -3 : counts as -1 at present
  131. -2 : show stderr
  132. -1 : user cancelled or error during dialog - no run
  133. 0  : run OK, do nothing special after
  134. 1  : show stdout
  135. 2  : show and select stdout
  136. > 2 : no action at present (counts as equivalent to 0)
  137. */
  138. void main(ACCPtr ac)
  139.     {
  140.     short    result;
  141.     long    _temp_a, _temp_c;
  142.     
  143.     /* initial setup of a4 */
  144.     asm
  145.         {
  146.         move.l    a4, _temp_a
  147.         move.l    a0, _temp_c
  148.         move.l    a0, a4
  149.         }
  150.     _app_a4 = _temp_a;
  151.     _code_a4 = _temp_c;
  152.     
  153.     /* Uncomment this for a version "sanity check": **************
  154.     if (ac->version < 0 || ac->version > 100)
  155.         {
  156.         OKStopAlert("Version number passed by calling application \
  157. is impossible. Please fix the application before trying again.");
  158.         ac->result = -1;
  159.         asm
  160.             {
  161.             move.l _app_a4, a4
  162.             }
  163.         return;
  164.         }
  165.     ********** end version check */
  166.     
  167.     gacc = *ac; /* make a global copy */
  168.     if (gacc.version <= 1)
  169.         {
  170.         gacc.DoEventLoopOnce_Ext = NULL;
  171.         gacc.GetAppClip_Ext = NULL;
  172.         }
  173.     if (HasDoEventLoopOnce())
  174.         gConcurrent = TRUE;
  175.     else
  176.         gConcurrent = FALSE;
  177.  
  178.     /* call 'true' main of code resource */
  179.     --------------CHANGE THIS-------------
  180.     result = InvokeResource();
  181.     
  182.     ac->result = result;
  183.     /****** unload non-main segments, for example
  184.     UnloadA4Seg(NewPtr);
  185.     UnloadA4Seg(strcmp);*****/
  186.     /* restore app's a4 */
  187.     asm
  188.         {
  189.         move.l _app_a4, a4
  190.         }
  191.     /* return nothing - communication via ac->result */
  192.     }
  193.  
  194. /* Extension functions. See Call_Resource.c for details
  195. on what these do. EnterAct supplies them all, but your application
  196. doesn't have to supply any of them, and your code resource should
  197. not rely on any of them being available.
  198.  
  199. A Boolean companion function for each extension reports whether
  200. the extension is available for use. 
  201. */
  202.  
  203. short InDictionary(char *tokenName)
  204.     {
  205.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  206.     short    ret;
  207.     
  208.     RestoreA4();
  209.     if (laccp->InDictionary_Ext != NULL)
  210.         ret = (*(laccp->InDictionary_Ext))(tokenName);
  211.     else
  212.         ret = 0;
  213.     SetUpA4();
  214.     return(ret);
  215.     }
  216.  
  217. Boolean HasInDictionary()
  218.     {
  219.     return(gacc.InDictionary_Ext != NULL);
  220.     }
  221.  
  222. Handle GetFrontText(Boolean getItAll)
  223.     {
  224.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  225.     Handle    ret;
  226.     
  227.     RestoreA4();
  228.     if (laccp->GetFrontText_Ext != NULL)
  229.         ret = (*(laccp->GetFrontText_Ext))(getItAll);
  230.     else
  231.         ret = NULL;
  232.     SetUpA4();
  233.     return(ret);
  234.     }
  235.  
  236. Boolean HasGetFrontText()
  237.     {
  238.     return(gacc.GetFrontText_Ext != NULL);
  239.     }
  240.  
  241. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  242.         short *vRefNumPtr, char *fileName, Boolean clearFlag)
  243.     {
  244.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  245.     
  246.     RestoreA4();
  247.     if (laccp->GetNextMultiFile_Ext != NULL)
  248.         (*(laccp->GetNextMultiFile_Ext))(panePtr, indexPtr,
  249.             vRefNumPtr, fileName, clearFlag);
  250.     else
  251.         *indexPtr = -1;
  252.     SetUpA4();
  253.     }
  254.  
  255. Boolean HasGetNextMultiFile()
  256.     {
  257.     return(gacc.GetNextMultiFile_Ext != NULL);
  258.     }
  259.  
  260. short OKStopAlert(Ptr cstringPtr)
  261.     {
  262.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  263.     short    ret;
  264.     
  265.     RestoreA4();
  266.     if (laccp->OKStopAlert_Ext != NULL)
  267.         ret = (*(laccp->OKStopAlert_Ext))(cstringPtr);
  268.     else
  269.         ret = 0;
  270.     SetUpA4();
  271.     return(ret);
  272.     }
  273.  
  274. Boolean HasOKStopAlert()
  275.     {
  276.     return(gacc.OKStopAlert_Ext != NULL);
  277.     }
  278.  
  279. void MemoryAlert()
  280.     {
  281.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  282.     
  283.     RestoreA4();
  284.     if (laccp->MemoryAlert_Ext != NULL)
  285.         (*(laccp->MemoryAlert_Ext))();
  286.     SetUpA4();
  287.     }
  288.  
  289. Boolean HasMemoryAlert()
  290.     {
  291.     return(gacc.MemoryAlert_Ext != NULL);
  292.     }
  293.  
  294. short GetScreenHeight()
  295.     {
  296.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  297.     short        ret;
  298.     
  299.     RestoreA4();
  300.     if (laccp->GetScreenHeight_Ext != NULL)
  301.         ret = (*(laccp->GetScreenHeight_Ext))();
  302.     else
  303.         ret = 342; /* minimum possible */
  304.     SetUpA4();
  305.     return(ret);
  306.     }
  307.  
  308. Boolean HasGetScreenHeight()
  309.     {
  310.     return(gacc.GetScreenHeight_Ext != NULL);
  311.     }
  312.  
  313. short GetScreenWidth()
  314.     {
  315.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  316.     short        ret;
  317.     
  318.     RestoreA4();
  319.     if (laccp->GetScreenWidth_Ext != NULL)
  320.         ret = (*(laccp->GetScreenWidth_Ext))();
  321.     else
  322.         ret = 512; /* minimum possible */
  323.     SetUpA4();
  324.     return(ret);
  325.     }
  326.  
  327. Boolean HasGetScreenWidth()
  328.     {
  329.     return(gacc.GetScreenWidth_Ext != NULL);
  330.     }
  331.  
  332. void SetWatchCursor()
  333.     {
  334.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  335.     
  336.     RestoreA4();
  337.     if (laccp->SetWatchCursor_Ext != NULL)
  338.         (*(laccp->SetWatchCursor_Ext))();
  339.     SetUpA4();
  340.     }
  341.  
  342. Boolean HasSetWatchCursor()
  343.     {
  344.     return(gacc.SetWatchCursor_Ext != NULL);
  345.     }
  346.  
  347. void DoEventLoopOnce()
  348.     {
  349.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  350.     
  351.     RestoreA4();
  352.     if (laccp->DoEventLoopOnce_Ext != NULL)
  353.         (*(laccp->DoEventLoopOnce_Ext))();
  354.     SetUpA4();
  355.     }
  356.  
  357. Boolean HasDoEventLoopOnce()
  358.     {
  359.     return(gacc.DoEventLoopOnce_Ext != NULL);
  360.     }
  361.  
  362. Handle GetTheClip()
  363.     {
  364.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  365.     Handle    ret;
  366.     
  367.     RestoreA4();
  368.     if (laccp->GetAppClip_Ext != NULL)
  369.         ret = (*(laccp->GetAppClip_Ext))();
  370.     else
  371.         ret = NULL;
  372.     SetUpA4();
  373.     return(ret);
  374.     }
  375.  
  376. Boolean HasGetTheClip()
  377.     {
  378.     return(gacc.GetAppClip_Ext != NULL);
  379.     }
  380.  
  381. // Note for version 3 the version is signalled by long extendID == 'VER3'
  382. // inside gacc -- for previous versions, the odds of this long being set
  383. // to exactly 'VER3' are very small.
  384.  
  385. short PutTheClip(char *newClipStr)
  386.     {
  387.     ACCPtr    laccp = &gacc; /* must use a local pointer */
  388.     short    ret = 0;
  389.     
  390.     RestoreA4();
  391.     if (laccp->extendID == 'VER3' && laccp->PutAppClip_Ext != NULL)
  392.         ret = (*(laccp->PutAppClip_Ext))(newClipStr);
  393.     SetUpA4();
  394.     return(ret);
  395.     }
  396.  
  397. Boolean HasPutTheClip(void)
  398.     {
  399.     return(gacc.extendID == 'VER3' && gacc.PutAppClip_Ext != NULL);
  400.     }
  401.  
  402.